import * as echarts from 'echarts';
export default class echartsFn {
  constructor(option) {
    this.echartsList = []
    this.echarts = echarts
  }

  // 初始化
  initEcharts(option) {
    if (!option) return
    const chartDom = document.getElementById(option.id)
    const myChart = echarts.init(chartDom, null, { renderer: 'svg' })
    myChart.setOption(option)
    this.echartsList.push({ id: option.id, myChart })
  }

  // 改变数据
  changeEcharts(id, option) {
    if (!id || !option) return
    const myChart = this.echartsList.filter(e => e.id == id)[0].myChart
    if (myChart) {
      myChart.clear()
      myChart.setOption(option)
    }
  }

  // 销毁
  removeEcharts(id = '') {
    if (id) {
      this.echartsList.forEach((item, index) => {
        if (item.id == id) {
          item.myChart.dispose()
          this.echartsList.splice(index, 1)
        }
      })
    } else {
      this.echartsList.forEach(item => {
        item.myChart.dispose()
      })
      this.echartsList = []
    }
  }
}